<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="uid"
        header-align="center"
        align="center"
        label="用户id">
      </el-table-column>
      <el-table-column
        prop="stuid"
        header-align="center"
        align="center"
        label="学号">
      </el-table-column>
      <el-table-column
        prop="password"
        header-align="center"
        align="center"
        label="密码md5加密">
      </el-table-column>
      <el-table-column
        prop="nickName"
        header-align="center"
        align="center"
        label="昵称">
      </el-table-column>
      <el-table-column
        prop="userStatus"
        header-align="center"
        align="center"
        label="是否正常">
        <template slot-scope="scope">
          <span v-if="scope.row.userStatus === 1">正常</span>
          <span v-else>异常</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="portrait"
        header-align="center"
        align="center"
        label="头像">
        <template slot-scope="scope">
          <el-avatar :src='scope.row.portrait' ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column
        prop="registerTime"
        header-align="center"
        align="center"
        label="注册时间">
      </el-table-column>
      <el-table-column
        prop="freeTime"
        header-align="center"
        align="center"
        label="解封时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" >操作</el-button>
          <el-button type="text" size="small" @click='resetPassword(scope.row.uid)'>重置密码</el-button>
          <el-button type="text" size="small" @click='showUserInfo(scope.row.uid)'>信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        useUid: '0',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
    },
    activated () {
      // this.getDataList()
      var _router = this.$route;
      var uid = this.$route.query.uid;
      console.log("_router", _router, "uid", uid)
      if(typeof(uid) !== 'undefined'){
        console.log("查询当前用户信息", uid)
        this.dataForm.key = uid
        this.useUid = '1'
        this.getDataList()
      }
      
      
      /*
      this.dataList=[
        {
          uid:1,
          stuid:'171110531',
          password:'xxx',
          nickName:'老张',
          userStatus:'1',
          portrait:'https://i1.hdslb.com/bfs/face/e0cc906bb531195e9ee9f3b575effdd2b056eaea.jpg',
          registerTime:'2021/04/12 19:05:01',
          freeTime:'2021/04/12 19:05:01',
        },{
          uid:2,
          stuid:'171110534',
          password:'xxx',
          nickName:'老朱',
          userStatus:'1',
          portrait:'https://i2.hdslb.com/bfs/face/e922d9dc09a2d21f1400ad0f02f75ecf1b3575c8.jpg',
          registerTime:'2021/04/12 19:05:01',
          freeTime:'2021/04/12 19:05:01',
        }
      ]*/
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        var data = {
          key:this.dataForm.key,
          page:''+this.pageIndex,
          limit:''+this.pageSize,
          sidx: '',
          order:'',
          useUid: this.useUid,
        }
        this.$http({
          url: this.$http.adornUrl('/user/login/user/list'),
          method: 'post',
          data: this.$http.adornData(data, false),
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.useUid =  '0'
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 重置密码
      resetPassword(uid){
        // 前端
        var index = this.getIndexFromDataList(uid)
        this.dataList[index].password='初始密码';
        // 后端
        this.$http({
          url: this.$http.adornUrl(`/user/login/user/update/reset/password/${uid}`),
          method: "post",
        }).then(({ data }) => {
          this.$message({
            type: "success",
            message: "成功!",
          });
          
        });
      },
      getIndexFromDataList(uid){
        let _dataList = this.dataList;
        for(var i = 0;i<_dataList.length;i++){
          if(_dataList[i].uid === uid){
            return i;
          }
        }
        return -1;
      },
      // 查看 用户 上传的信息
      showUserInfo(){
        
      }
      
      
      
    }
  }
</script>
